package com.example.administrator.szb.fragments.fragment_forXM;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.ProductDetail2Activity;
import com.example.administrator.szb.activity.ProductDetailActivity;
import com.example.administrator.szb.adapter.XMAdapter;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.bean.ProjectBase;
import com.example.administrator.szb.bean.ProjectDetail;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by stone on 2018/1/17.
 */

public class FragmentXM2 extends MVPBaseFragment {
    private ListView listView;
    private SmartRefreshLayout refreshLayout;
    private TextView text_view_error;
    private ImageView nodata_img;
    private View nodata_ll,error_nets_ll;
    private int type;//type =1,我发出的项目，type =2 ,我收到的项目
    ProjectBase projectBase;
    private int current_page = 1;
    private int per_page = 10;
    List<ProjectDetail> list = new ArrayList<>();//a申请我的  b我申请的
    private XMAdapter adapter;
    private boolean isFirstEnter = true;//第一次进入刷新
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.xmgl_fragment,null);
    }

    @Override
    protected void initView(View view) {
        text_view_error = view.findViewById(R.id.text_view_error);
        if (type==1){
            text_view_error.setText("您还没有发过项目");
        }else {
            text_view_error.setText("您还没有收到项目");
        }

        nodata_ll = view.findViewById(R.id.nodata_ll);
        error_nets_ll = view.findViewById(R.id.error_nets_ll);
        listView = view.findViewById(R.id.xmgl_recyclerview);
        refreshLayout = view.findViewById(R.id.xmgl_swiperefreshlayout);
    }

    @Override
    protected void initEvent() {
        error_nets_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoRefresh();
                current_page = 1;
                requestData(true,current_page);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            refreshLayout.autoRefresh();
        }
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //refreshlayout.setLoadmoreFinished(false);//恢复上拉状态
                // Toasts.show(getActivity(), "加载更多啊", Toast.LENGTH_SHORT);
                int page = current_page+1;
                requestData(false,page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page = 1;
                requestData(true,current_page);
                refreshLayout.setEnableLoadMore(true);
                //refreshlayout.finishRefresh();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                godetail(type,list.get(position).getId());
            }
        });

    }
    public void godetail(int types,int id){
        Class c;
        if (types==1){//我发出的
            c = ProductDetailActivity.class;
        }else {//我收到的
            c = ProductDetail2Activity.class;
        }
        Intent intent = new Intent(getActivity(),c);
        intent.putExtra("dd_id",id);
        intent.putExtra("genre",types);
        startActivity(intent);
    }
    @Override
    protected void initData() {
        if (adapter==null){
            adapter = new XMAdapter(context,list,type);
            listView.setAdapter(adapter);
        }
    }
    private void requestData(final boolean isRefresh,final int page){

        //隐藏无数据，显示listView
        listView.setVisibility(View.VISIBLE);
        nodata_ll.setVisibility(View.GONE);
        error_nets_ll.setVisibility(View.GONE);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId()+"");
        map.put("current_page",""+page);
        map.put("per_page",""+per_page);
        String url = URLAddress.URLBASE + URLAddress.URL_XM;
        HttpUtils.post(activity,url, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                try {
                    projectBase = JSON.parseObject(str,new TypeReference<ProjectBase>(){});
                } catch (Exception e) {
                    str = str.replace("\"indentInfo\":[]","\"indentInfo\":null");
                    str = str.replace("\"a\":null","\"a\":[]");
                    str = str.replace("\"b\":null","\"b\":[]");
                    QTLog.e(str);
                    projectBase = JSON.parseObject(str,new TypeReference<ProjectBase>(){});
                }
                if (projectBase.getA()==null){
                    projectBase.setA(new ArrayList<ProjectDetail>());
                }
                if (projectBase.getB()==null){
                    projectBase.setB(new ArrayList<ProjectDetail>());
                }
                QTLog.e(type+"a"+projectBase.getA().size());
                QTLog.e(type+"B"+projectBase.getB().size());
                List listTmp;
                //type =1,我发出的项目(b我申请的)，type =2 ,我收到的项目(a申请我的)
                //a申请我的  b我申请的

                if (type==1){
                    listTmp = projectBase.getB();
                }else {
                    listTmp = projectBase.getA();
                }


                if (isRefresh){
                    //是刷新
                    refreshLayout.finishRefresh();
                    list.clear();
                }else {
                    refreshLayout.finishLoadmore();
                    //是加载更多
                    if (listTmp.size()<per_page){
                        //说明没有更多了
                        refreshLayout.setEnableLoadMore(false);
                    }else {
                        current_page++;
                    }

                }
                list.addAll(listTmp);
                QTLog.e(""+list.size());
                if (list.size()==0){
                    //显示无数据，隐藏listView
                    listView.setVisibility(View.GONE);
                    nodata_ll.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(int requestCode, String str) {
                listView.setVisibility(View.GONE);
                nodata_ll.setVisibility(View.GONE);
                error_nets_ll.setVisibility(View.VISIBLE);
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
            }
        });


    }
}
